#!  python
# -*- coding: UTF-8 -*-
'''
@Author ：Star
@Date ：2022/11/22
'''
import unittest
import requests
import time

class TestAPI(unittest.TestCase):
    base_url = "http://182.151.48.185:8098/api/private/v1/"

    @classmethod
    def setUpClass(cls):
        cls.token =cls.login("admin","123456")["token"]
        cls.header = {"Authorization": cls.token}

    # 登录
    @classmethod
    def login(self,username,password):
        url = self.base_url + "login"
        data = {  # 字典
            "username": username,
            "password": password
        }
        h = {
            "content-type": "application/json"
        }
        req = requests.post(url, json=data, headers=h)
        print(req.json()["data"]["token"])
        return req.json()["data"]


    def test_api(self):
        print("-" * 20)
        url = self.base_url + "users"
        p = {
            "pagenum": 1,
            "pagesize": 10
        }
        req = requests.get(url, headers=self.header, params=p)
        print(req.json())

    # 添加用户
    def test_addUser(self):
        url = self.base_url + "users"
        now = time.localtime()
        formatTime = time.strftime("%Y%m%d%H%M%S",now)
        username= "star"+formatTime
        data = {
            "username": username,
            "password": "12345",
            "email": "2570257344@qq.com"
        }
        res = requests.post(url,data=data,headers=self.header)
        # print("456",res.json())
        self.assertIn("创建成功",res.text)
        # print(res.json()["data"]["id"])
        #  获取新添加的ID
        # return res.json()["data"]["id"]

    # 根据 ID 查询用户信息`
    def test_query(self,id="500" ):
        url = self.base_url + "users/"+id
        res = requests.get(url,headers=self.header)
        print(res.json())
        self.assertIn("获取成功",res.text)
        # requests.put

if __name__ == '__main__':
    unittest.main()
